<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\user;

use app\data\model\DataQiyeBiaoqian;
use app\data\model\DataUser as DataNewsItem;
use app\data\service\NewsService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 服务员列表
 * Class Item
 * @package app\data\controller\news
 */
class Fuwuguan extends Controller
{
    /**
     * 服务员列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';
        DataNewsItem::mQuery($this->get)->layTable(function ()
        {
            $this->title = '服务员列表';
        }, function (QueryHelper $query)
        {
            $query->like('nickname,phone')->dateBetween('create_at');
            $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0, 'sf' => 2]);
        });
    }

    /**
     * 列表数据处理
     * @param  array  $data
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as &$item) {
            $item['qybq_txt']  = '';
            $item['top_name']  = '';
            $item['top_phone'] = '';
            if (!empty($item['qybq'])) {
                $qybq_array       = explode(',', $item['qybq']);
                $list             = DataQiyeBiaoqian::query()->whereIn('id', $qybq_array)->column('name');
                $item['qybq_txt'] = implode(',', $list);
            }
            if (!empty($item['top_ld'])) {
                $top_ld            = Db::name("data_user")->where('id', $item['top_ld'])->find();
                $item['top_name']  = $top_ld['nickname'];
                $item['top_phone'] = $top_ld['phone'];
            }
        }
    }

    /**
     * 添加文章内容
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _add_form_filter(array &$data)
    {

        if ($this->request->isPost()) {


            $is_cz = Db::name("data_user")->where('phone',$data['phone'])->where('deleted',0)->count();

            if(!empty($is_cz)){
                $this->error('手机号已经存在！', 'javascript:history.back()');
            }


            if(!empty($data['password'])){
                $data['password'] = md5($data['password']);
            }

            $data['sf'] = 2;

        }else{


        }
    }


    protected function _edit_form_filter(array &$data)
    {

        if ($this->request->isPost()) {

            $is_cz = Db::name("data_user")
                ->where('phone', $data['phone'])
                ->where('deleted', 0)
                ->where('id', '<>', $data['id']) // 排除特定的 user_id
                ->count();
//
            if(!empty($is_cz)){
                $this->error('手机号已经存在！', 'javascript:history.back()');
            }


            if(!empty($data['password'])){
                $data['password'] = md5($data['password']);
            }else{
                unset($data['password']);
            }

            $data['sf'] = 2;

        }else{
            $data['password'] = '';

        }
    }


    /**
     * 表单结果处理
     * @param  boolean  $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     */
    public function remove()
    {
        DataNewsItem::mDelete();
    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }

    public function getQybqList()
    {
        $biaoqian = Db::name('data_qiye_biaoqian')->where('deleted', 0)->select()->toArray();
        return_msg(200, 'success', $biaoqian);
    }


    //绑定
    public function bangding()
    {

        $id = input('id');
        if ($this->request->isGet()) {
            $this->title         = '绑定领导';
            $this->fuwuguan_list = Db::name('data_user')->where('id', '<>', $id)->where('deleted', 0)->where('sf', 2)->order('nickname desc')->select()->toArray();
            DataNewsItem::mForm('bangding');
        } else {
            $top_ld = input('top_ld');
            //            $top_user = Db::name("data_user")->where('id',$id)->find();
            Db::name("data_user")->where('id', $id)->update(['top_ld' => $top_ld]);
            $this->success('绑定成功！', 'javascript:history.back()');
        }
    }
}
